/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChartPanel.java
 *
 * Created on 24 avr. 2011, 20:56:35
 */

package modbuspal.automation;

import java.awt.BorderLayout;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 * A panel that contains a chart, tracking the values of
 * an automation
 * @author nnovic
 */
public class AutomationChart
extends javax.swing.JPanel
implements AutomationExecutionListener
{
    private XYSeriesCollection collection;
    private JFreeChart chart;
    private XYSeries xySeries;


    /** Creates new form ChartPanel 
     * @param source the automation that produces the value to draw on the chart
     */
    public AutomationChart(Automation source)
    {
        initComponents();
        createChart();
        source.addAutomationExecutionListener(this);
    }

    private void createChart()
    {
        // Create the chart:
        collection = new XYSeriesCollection();
        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true,false);

        xySeries = new XYSeries("Automation");
        collection.addSeries(xySeries);

        chart = ChartFactory.createXYLineChart(null,"Time","Value",collection,PlotOrientation.VERTICAL,false,false,false);

        // Customize the chart:
        XYPlot plot = (XYPlot)chart.getPlot();
        plot.setRenderer( renderer );

        ChartPanel panel = new ChartPanel(chart);
        add( panel, BorderLayout.CENTER);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setLayout(new java.awt.BorderLayout());
    }// </editor-fold>//GEN-END:initComponents


    @Override
    public void automationHasStarted(Automation source) 
    {
        xySeries.clear();
    }

    @Override
    public void automationHasEnded(Automation source) {
    }

    @Override
    public void automationValueHasChanged(Automation source, double time, double value) 
    {
        xySeries.addOrUpdate(time, value);
    }

    @Override
    public void automationReloaded(Automation source) 
    {
        //xySeries.clear();
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
